//
//  ByAccountDetailVC.m
//  recordworktool_ios
//
//  Created by mc on 2017/12/9.
//  Copyright © 2017年 ZhangZhanbin. All rights reserved.
//

#import "ByAccountDetailVC.h"
#import "ByAccountDetailView.h"
#import "ByAccountControl.h"
#import "JSONKit.h"


@interface ByAccountDetailVC ()

@end

@implementation ByAccountDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addLeftBackBarButtonItem];
    self.title = @"明细";
    [self initViews];
    // Do any additional setup after loading the view.
}

- (void)initViews{
    self.view.backgroundColor = BY_BACKGROUND_COLOR;
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, SCREEN_HEIGHT - STATUS_NAV - 10)];
    [self.view addSubview:scrollView];
   
    //点工
    if ([self.netModel.item_type_key  isEqualToString:@"00S002003"]) {
        [self initFirstViews:scrollView];
        
    //包工
    }else if ([self.netModel.item_type_key  isEqualToString:@"00S002002"]){
        [self initSecondViews:scrollView];
        
    }else{
        
        [self initOtherViews:scrollView];
    }
   
    
}

//点工
- (void)initFirstViews:(UIScrollView *)scrollView{
     CGFloat height = 45;
    if (SCREEN_HEIGHT < height * 11 + STATUS_NAV + 10 + 150) {
        scrollView.contentSize = CGSizeMake(scrollView.size.width, height * 11  + 150);
    }
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(30, 10, SCREEN_WIDTH - 60, 11 * height)];
    [scrollView addSubview:view];
    view.layer.borderColor = BY_THEME_COLOR.CGColor;
    view.layer.borderWidth = 1;
    view.layer.cornerRadius = 10;
    view.layer.masksToBounds = YES;
    
    NSString *normal_work_time;
    NSString *over_work_time ;
    NSString *hourly_wage ;
    
    NSMutableDictionary *dic;
    if (self.netModel.extra && self.netModel.extra.length > 0 && ![self.netModel.extra isEqualToString:@"[]"]) {
        dic = [self.netModel.extra mutableObjectFromJSONString];
        normal_work_time = dic[@"normal_worktime"];
        over_work_time = dic[@"over_worktime"];
        hourly_wage = dic[@"hourly_wage"];
    }

    NSArray *itemsAry = @[@"工头姓名",@"工头手机号",@"工人姓名",@"工人手机号",@"项目",@"工时",@"加班时长",@"记工单位",@"加班记工单位",@"工资单位",@"合计"];
    
    for (int i = 0; i < 11; i++) {
        ByAccountDetailView *detailView = [[ByAccountDetailView alloc] initWithFrame:CGRectMake(1, i * height, view.size.width - 2, height)];
        [view addSubview:detailView];
        detailView.titleLabel.text = itemsAry[i];
        if (i == 10) {
            detailView.line.hidden = YES;
        }
        if (i == 0) {
            detailView.valueLabel.text = self.netModel.overseer_name;
        }else if (i == 1){
            detailView.valueLabel.text = self.netModel.overseer_mobile;
        }else if (i == 2){
            detailView.valueLabel.text = self.netModel.worker_name;
        }else if (i == 3){
            detailView.valueLabel.text = self.netModel.worker_mobile;
        }else if (i == 4){
            detailView.valueLabel.text = self.netModel.project_title;
        }else if (i == 5){
            detailView.valueLabel.text = [NSString stringWithFormat:@"%@时",normal_work_time];
        }else if (i == 6){
            detailView.valueLabel.text = [NSString stringWithFormat:@"%@时", over_work_time];
        }else if (i == 7){
            detailView.valueLabel.text = [NSString stringWithFormat:@"8时/工"];
        }else if (i == 8){
            detailView.valueLabel.text = [NSString stringWithFormat:@"6时/工"];
        }else if (i == 9){
            detailView.valueLabel.text = [NSString stringWithFormat:@"400元／工"];
        }else if (i == 10){
            detailView.valueLabel.text = [NSString stringWithFormat:@"%.2f", self.netModel.money.floatValue];
        }
    }
    
    
    ByAccountControl *leftControl = [[ByAccountControl alloc] init];
    [scrollView addSubview:leftControl];
    [leftControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(scrollView.mas_centerX).offset(-30);
        make.top.equalTo(view.mas_bottom).offset(40);
        make.size.mas_equalTo(CGSizeMake(100, 45));
    }];
    leftControl.tag = 100;
    leftControl.imageView.image = [UIImage imageNamed:@"tongguo"];
    leftControl.titleLabel.text = @"通过";
    
    ByAccountControl *rightControl = [[ByAccountControl alloc] init];
    [scrollView addSubview:rightControl];
    [rightControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(scrollView.mas_centerX).offset(30);
        make.top.equalTo(view.mas_bottom).offset(40);
        make.size.mas_equalTo(CGSizeMake(100, 45));
    }];
    rightControl.tag = 200;
    rightControl.imageView.image = [UIImage imageNamed:@"cunyi"];
    rightControl.titleLabel.text = @"存疑";
    [leftControl addTarget:self action:@selector(controlAction:) forControlEvents:UIControlEventTouchUpInside];
    [rightControl addTarget:self action:@selector(controlAction:) forControlEvents:UIControlEventTouchUpInside];
    
    if (self.type == 0) {
        leftControl.hidden = YES;
        rightControl.hidden = YES;
    }
    
}

//包工
- (void)initSecondViews:(UIScrollView *)scrollView{
    
    CGFloat height = 45;
    if (SCREEN_HEIGHT < height * 9 + STATUS_NAV + 10 + 150) {
        scrollView.contentSize = CGSizeMake(scrollView.size.width, height * 9  + 150);
    }
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(30, 10, SCREEN_WIDTH - 60, 9 * height)];
    [scrollView addSubview:view];
    view.layer.borderColor = BY_THEME_COLOR.CGColor;
    view.layer.borderWidth = 1;
    view.layer.cornerRadius = 10;
    view.layer.masksToBounds = YES;
    
    NSString *count;
    
    NSMutableDictionary *dic;
    if (self.netModel.extra && self.netModel.extra.length > 0 && ![self.netModel.extra isEqualToString:@"[]"]) {
        dic = [self.netModel.extra mutableObjectFromJSONString];
        count = dic[@"count"];
       
    }
    
    NSArray *itemsAry = @[@"工头姓名",@"工头手机号",@"工人姓名",@"工人手机号",@"项目",@"子项目",@"数量",@"记工单位",@"合计"];
    
    for (int i = 0; i < 9; i++) {
        ByAccountDetailView *detailView = [[ByAccountDetailView alloc] initWithFrame:CGRectMake(1, i * height, view.size.width - 2, height)];
        [view addSubview:detailView];
        detailView.titleLabel.text = itemsAry[i];
        if (i == 8) {
            detailView.line.hidden = YES;
        }
        if (i == 0) {
            detailView.valueLabel.text = self.netModel.overseer_name;
        }else if (i == 1){
            detailView.valueLabel.text = self.netModel.overseer_mobile;
        }else if (i == 2){
            detailView.valueLabel.text = self.netModel.worker_name;
        }else if (i == 3){
            detailView.valueLabel.text = self.netModel.worker_mobile;
        }else if (i == 4){
            detailView.valueLabel.text = self.netModel.project_title;
        }else if (i == 5){
            detailView.valueLabel.text = self.netModel.sub_title;
        }else if (i == 6){
            detailView.valueLabel.text = [NSString stringWithFormat:@"%@", count];
        }else if (i == 7){
            detailView.valueLabel.text = [NSString stringWithFormat:@"8时/工"];
        }else if (i == 8){
            detailView.valueLabel.text = [NSString stringWithFormat:@"%.2f", self.netModel.money.floatValue];
        }
    }
    
    
    ByAccountControl *leftControl = [[ByAccountControl alloc] init];
    [scrollView addSubview:leftControl];
    [leftControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(scrollView.mas_centerX).offset(-30);
        make.top.equalTo(view.mas_bottom).offset(40);
        make.size.mas_equalTo(CGSizeMake(100, 45));
    }];
    leftControl.tag = 100;
    leftControl.imageView.image = [UIImage imageNamed:@"tongguo"];
    leftControl.titleLabel.text = @"通过";
    
    ByAccountControl *rightControl = [[ByAccountControl alloc] init];
    [scrollView addSubview:rightControl];
    [rightControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(scrollView.mas_centerX).offset(30);
        make.top.equalTo(view.mas_bottom).offset(40);
        make.size.mas_equalTo(CGSizeMake(100, 45));
    }];
    rightControl.tag = 200;
    rightControl.imageView.image = [UIImage imageNamed:@"cunyi"];
    rightControl.titleLabel.text = @"存疑";
    [leftControl addTarget:self action:@selector(controlAction:) forControlEvents:UIControlEventTouchUpInside];
    [rightControl addTarget:self action:@selector(controlAction:) forControlEvents:UIControlEventTouchUpInside];
    
    if (self.type == 0) {
        leftControl.hidden = YES;
        rightControl.hidden = YES;
    }
}


//借支 和 结算
- (void)initOtherViews:(UIScrollView *)scrollView{
    
    CGFloat height = 45;
    if (SCREEN_HEIGHT < height * 7 + STATUS_NAV + 10 + 150) {
        scrollView.contentSize = CGSizeMake(scrollView.size.width, height * 7  + 150);
    }
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(30, 10, SCREEN_WIDTH - 60, 7 * height)];
    [scrollView addSubview:view];
    view.layer.borderColor = BY_THEME_COLOR.CGColor;
    view.layer.borderWidth = 1;
    view.layer.cornerRadius = 10;
    view.layer.masksToBounds = YES;
    
    NSArray *itemsAry;
     //借支
    if ([self.netModel.item_type_key  isEqualToString:@"00S002004"]) {
        
        itemsAry = @[@"工头姓名",@"工头手机号",@"工人姓名",@"工人手机号",@"项目",@"借支金额",@"合计"];
     //结算
    }else{
        
        itemsAry = @[@"工头姓名",@"工头手机号",@"工人姓名",@"工人手机号",@"项目",@"结算金额",@"合计"];
    }
    
    for (int i = 0; i < 7; i++) {
        ByAccountDetailView *detailView = [[ByAccountDetailView alloc] initWithFrame:CGRectMake(1, i * height, view.size.width - 2, height)];
        [view addSubview:detailView];
        detailView.titleLabel.text = itemsAry[i];
        if (i == 6) {
            detailView.line.hidden = YES;
        }
        if (i == 0) {
            detailView.valueLabel.text = self.netModel.overseer_name;
        }else if (i == 1){
            detailView.valueLabel.text = self.netModel.overseer_mobile;
        }else if (i == 2){
            detailView.valueLabel.text = self.netModel.worker_name;
        }else if (i == 3){
            detailView.valueLabel.text = self.netModel.worker_mobile;
        }else if (i == 4){
            detailView.valueLabel.text = self.netModel.project_title;
        }else if (i == 5){
            detailView.valueLabel.text =  [NSString stringWithFormat:@"%.2f", self.netModel.money.floatValue];
        }else if (i == 6){
            detailView.valueLabel.text = [NSString stringWithFormat:@"%.2f", self.netModel.money.floatValue];
        }
    }
    
    
    ByAccountControl *leftControl = [[ByAccountControl alloc] init];
    [scrollView addSubview:leftControl];
    [leftControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(scrollView.mas_centerX).offset(-30);
        make.top.equalTo(view.mas_bottom).offset(40);
        make.size.mas_equalTo(CGSizeMake(100, 45));
    }];
    leftControl.tag = 100;
    leftControl.imageView.image = [UIImage imageNamed:@"tongguo"];
    leftControl.titleLabel.text = @"通过";
    
    ByAccountControl *rightControl = [[ByAccountControl alloc] init];
    [scrollView addSubview:rightControl];
    [rightControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(scrollView.mas_centerX).offset(30);
        make.top.equalTo(view.mas_bottom).offset(40);
        make.size.mas_equalTo(CGSizeMake(100, 45));
    }];
    rightControl.tag = 200;
    rightControl.imageView.image = [UIImage imageNamed:@"cunyi"];
    rightControl.titleLabel.text = @"存疑";
    [leftControl addTarget:self action:@selector(controlAction:) forControlEvents:UIControlEventTouchUpInside];
    [rightControl addTarget:self action:@selector(controlAction:) forControlEvents:UIControlEventTouchUpInside];
    
    if (self.type == 0) {
        leftControl.hidden = YES;
        rightControl.hidden = YES;
    }
    
}

- (void)controlAction:(UIControl *)control{
    if (control.tag == 100) {
        NSLog(@"通过");
        NSLog(@"ID = %@", self.netModel.ID);
        ByNetModel *netModel = [[ByNetModel alloc] init];
        netModel.ID = self.netModel.ID;
        netModel.audit_status = @"1";
        netModel.operator_uid = BY_USER.uid;
        [ByApiRecord audit:netModel success:^(ByNetBaseResult *result) {
             [ByCoreToolAlert showSuccessWithText:@"操作成功"];
            [self performSelector:@selector(test) withObject:nil afterDelay:0.5];
        } failure:^(ByNetBaseResult *result) {
            [ByCoreToolAlert showFailureWithText:result.msg];
        }];
        
    }else if (control.tag == 200){
        
        if (self.netModel.audit_status.integerValue == 2) {
            [ByCoreToolAlert showOnlyText:@"当前正是存疑状态，没必要操作"];
            return;
        }
        NSLog(@"存疑");
        ByNetModel *netModel = [[ByNetModel alloc] init];
        netModel.ID = self.netModel.ID;
        netModel.audit_status = @"2";
        netModel.operator_uid = BY_USER.uid;
        [ByApiRecord audit:netModel success:^(ByNetBaseResult *result) {
             [ByCoreToolAlert showSuccessWithText:@"操作成功"];
            [self performSelector:@selector(test) withObject:nil afterDelay:0.5];
        } failure:^(ByNetBaseResult *result) {
            [ByCoreToolAlert showFailureWithText:result.msg];
        }];
    }
}

- (void)test{
    
   
    [self leftItemsAction:0];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
